/*
 * Created on 2005-6-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package leeon.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class HtmlUtil {

	public static void main(String[] args) throws Exception {
		//String html = "xsdfsdfsdf <a sdfsdf href = \"aaaddd\">xxx</a>";
		//System.out.println(findHref(html, "xxx", "\""));
		String path = "c:\\NG\\2005\\2005-09-87.txt";		
		saveBytesToFile((path+"\r").getBytes(), path, HtmlUtil.WHEN_EXIST_APPEND);
	}
	
	public static String findHref(String html, String name, String delim) {
		int s = html.indexOf(name);
		s = html.lastIndexOf("<a", s);
		s = html.indexOf("href", s);
		s = html.indexOf(delim, s)+1;
		int e = html.indexOf(delim, s);		
		return html.substring(s, e).trim();
	}
	
	public static String findTag(String html, String startTag, String endTag) {
		int s = html.indexOf(startTag);
		if (s == -1) return null;
		
		s += startTag.length();
		
		int e = html.indexOf(endTag, s);
		if (e == -1) return null;
		
		return html.substring(s, e).trim();
	}
	
	public static String findTag(String html, String startTag) {
		int s = html.indexOf(startTag);
		if (s == -1) return null;
		
		s += startTag.length();
				
		return html.substring(s).trim();
	}
	
	public static String findText(String html, String text, int start) {
		int s = html.indexOf(text, start);
		if (s == -1) return null;
		
		s = html.lastIndexOf(">", s);
		if (s == -1) return null;
		
		int e = html.indexOf("<", s);
		if (e == -1) return null;
		
		return html.substring(s+1, e).trim();
	}
	
	public static String[] findTr(String html, String text) {
		return findTr(html, text, 0);
	}
	
	public static String[] findTr(String html, String text, int start) {
		int s = html.indexOf(text, start);
		if (s == -1) return null;
		
		s = html.lastIndexOf("<TR", s);
		if (s == -1) return null;
		
		int e = html.indexOf("</TR>", s);
		if (e == -1) return null;
		
		String ret = html.substring(s, e+5);
		//去注释
		ret = ret.replaceAll("<!--.*-->", "");
		//去空白
		ret = ret.replaceAll("\\s*", "");
		//换td
		ret = ret.replaceAll("</TD[^<>]*>", ",");
		//去其他
		ret = ret.replaceAll("<[^<>]*>", "");
		return ret.split(",");
	}
	
	public static String[] findTrLower(String html, String text, int limit) {
		int s = html.indexOf(text);
		if (s == -1) return null;
		
		s = html.lastIndexOf("<tr", s);
		if (s == -1) return null;
		
		int e = html.indexOf("</tr>", s);
		if (e == -1) return null;
		
		String ret = html.substring(s, e+5);
		//去注释
		ret = ret.replaceAll("<!--[^!]*-->", "");
		//去空白
		ret = ret.replaceAll("\\s*", "");
		//换逗号
		ret = ret.replaceAll(",", "，");
		//换td
		ret = ret.replaceAll("</td[^<>]*>", ",");
		//去其他
		ret = ret.replaceAll("<[^<>]*>", "");
		//去空格
		ret = ret.replaceAll("&nbsp;", "");
		return ret.split(",", limit);
	}
	
	public static String[] findTrLower(String html, String text) {
		return findTrLower(html, text, 0);
	}
	
	public static String[] findTable(String table) {
		int s = 0, e = 0;
		List<String> ret = new ArrayList<String>();
		while ((s = table.indexOf("<tr", e)) != -1) {
			e = table.indexOf("</tr>", s) + 5;
			ret.add(table.substring(s, e));
		}
		return ret.toArray(new String[0]);
	}
	
	public static final int WHEN_EXIST_OVERWRITE = 0;
	public static final int WHEN_EXIST_BACKUP = 1;
	public static final int WHEN_EXIST_APPEND = 2;
		
	public static void saveBytesToFile(byte[] bytes, String path, int whenExist) throws IOException {
		File file = new File(path);
		boolean append = false;
		if (file.exists()) {
			if (whenExist == HtmlUtil.WHEN_EXIST_OVERWRITE) {

			} else if (whenExist == HtmlUtil.WHEN_EXIST_APPEND) {
				append = true;
			} else if (whenExist == HtmlUtil.WHEN_EXIST_BACKUP) {
				file.renameTo(new File(path+".bak"));
				file.createNewFile();
			}
		} else {
			if (!file.getParentFile().exists())
				file.getParentFile().mkdirs();
			file.createNewFile();
		}
		
		FileOutputStream fos = new FileOutputStream(file, append);
		fos.write(bytes);
		fos.close();
	}
}
